<%@ page contentType="text/html; charset=utf-8" %>
<%@ taglib prefix="tiles" uri="http://struts.apache.org/tags-tiles"%>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%>
<%@ taglib prefix="c_rt" uri="http://java.sun.com/jstl/core_rt"%>
<%@ taglib prefix="fmt" uri="http://java.sun.com/jsp/jstl/fmt"%>
<%@ taglib prefix="fmt_rt" uri="http://java.sun.com/jstl/fmt_rt"%>
<%@ taglib prefix="fn" uri="http://java.sun.com/jsp/jstl/functions"%>
<%@ taglib prefix="sql" uri="http://java.sun.com/jsp/jstl/sql"%>
<%@ taglib prefix="x" uri="http://java.sun.com/jsp/jstl/xml"%>
<%@ taglib prefix="x_rt" uri="http://java.sun.com/jstl/xml_rt"%>
<%@ taglib prefix="wr_fn" uri="http://njo.org/taglib/functions"%>

<%@page import="org.tmatesoft.svn.core.*" %>
<%@page import="org.tmatesoft.svn.core.auth.*" %>
<%@page import="org.tmatesoft.svn.core.internal.io.dav.*" %>
<%@page import="org.tmatesoft.svn.core.internal.io.svn.*" %>
<%@page import="org.tmatesoft.svn.core.io.*" %>
<%@page import="org.tmatesoft.svn.core.wc.*" %>
<%@page import="java.util.*" %>
<%@page import="org.njo.webapp.root.model.RemoteUser" %>
<%@page import="org.njo.webapp.root.utility.SystemContext" %>


<%
  RemoteUser remoteUser = (RemoteUser)session.getAttribute("remoteUser");

  // String url = "http://localhost/repository/Development";
  // String user = remoteUser.getName();
  // String passwd = remoteUser.getPassword();
  String url = SystemContext.getProperty("repos.server.url","http://localhost/repos/Development---");
  String user = remoteUser.getName();
  String passwd = remoteUser.getPassword();
  
  String APACHE_DOWNLOAD_PATH = SystemContext.getProperty("repos.download.path", "/repos/Development---");
  String FILE_SEPARATOR = "/";

    String f = request.getParameter("f");
    if (f==null) {
      f = "";
    }

    // 相对路径
    String r = request.getParameter("r");
    if (r==null) {
      r = "";
    }
    
if ("..".equals(f)) {
  // 取得父目录路径
  int pos = r.lastIndexOf(FILE_SEPARATOR);
  r = r.substring(0,pos);
  pos = r.lastIndexOf(FILE_SEPARATOR,pos);
  r = r.substring(0,pos+1);
} else {
  // 算出请求的路径
  r = r + f + FILE_SEPARATOR;
}
%>

<form name="f1" action="${pageContext.request.contextPath}/svn.tiles" method="post">
  <h3><a name="welcome"><%=r%></a></h3>
  <input type="hidden" name="f" value=""/>
  <input type="hidden" name="r" value="<%=r%>"/>
  <table class="forum" width="98%">
  <col width="*"/>
  <col width="80"/>
  <col width="60"/>
  <col width="140"/>
  <col width="100"/>
  <col width="140"/>
  <tbody>
  <tr>
    <th>Name</th>
    <th>Author</th>
    <th>Revision</th>
    <th>Last modified</th>
    <th>Size</th>
    <th>Comment</th>
  </tr>
<%
  // 取得本地真实的路径
  String realPath = url + r;
  //for DAV (over http and https)
  DAVRepositoryFactory.setup();
  //for SVN (over svn and svn+ssh)
  SVNRepositoryFactoryImpl.setup();
  SVNRepository repository = null;
  try {
    repository = SVNRepositoryFactory.create(SVNURL.parseURIEncoded(realPath));
    ISVNAuthenticationManager authManager = SVNWCUtil.createDefaultAuthenticationManager(user, passwd);
    repository.setAuthenticationManager(authManager);
    long revision = repository.getLatestRevision();
%>
<% if (!FILE_SEPARATOR.equals(r)) {%>
      <tr>
        <td class="even" colspan="6">[<a href="javascript:document.f1.f.value = '..';document.f1.submit();">...</a>]</td>
      </tr>
<% }%>
   
<%
    List directoryEntries = new ArrayList();
    SVNDirEntry directoryEntry = repository.getDir("", revision, false, directoryEntries);
    java.text.SimpleDateFormat dateFormat = new java.text.SimpleDateFormat("yyyy/MM/dd HH:mm");
    for (int i = 0 ; i < directoryEntries.size(); i++) {
        SVNDirEntry childEntry = (SVNDirEntry) directoryEntries.get(i);
%>
    <tr class="<%=i%2==1 ? "even" : "odd" %>">
      <td>
      <% if (SVNNodeKind.DIR==childEntry.getKind()) {%>
        <a title="<%=getMessage(repository, childEntry.getRevision())%>" href="javascript:document.f1.f.value = '<%=childEntry.getName()%>';document.f1.submit();"><%=childEntry.getName() + "/"%></a>
      <% } else { %>
        <a title="<%=getMessage(repository, childEntry.getRevision())%>" href="<%=APACHE_DOWNLOAD_PATH + r + childEntry.getName()%>"><%=childEntry.getName()%></a>
      <% } %>
      </td>
      <td class="right"><%=childEntry.getAuthor() %></td>
      <td class="right"><%=childEntry.getRevision()%></td>
      <td class="right"><%=dateFormat.format(childEntry.getDate())%></td>
      <td class="right"><%=SVNNodeKind.FILE==childEntry.getKind() ? convertFileSize(childEntry.getSize()) : "&nbsp;" %></td>
      <td class="left"><%=paddedString(getMessage(repository, childEntry.getRevision()), 7)%>...</td>
    </tr>
<%
    }
  } catch (SVNException e) {
      %> <%= e + "====" %> 
<%
    e.printStackTrace();
  }

%>
  </tbody>
  </table>
</form>
<%!

  /**
   * 取得指定revision的注释.
   * 
   */
  private String getMessage(SVNRepository repository, long revision) {
    try {
      SVNLogEntryHandler handler = new SVNLogEntryHandler();
      repository.log(new String[] {""}, revision, revision, true, false, 1, handler);
      return handler.getMessage();
    } catch (SVNException e) {
      e.printStackTrace();
      return "";
    }
  }
  
  /**
   * 
   * 
   */
  class SVNLogEntryHandler implements ISVNLogEntryHandler {
    private SVNLogEntry logEntry;
    public SVNLogEntryHandler() {
      super();
    }
    public void handleLogEntry(SVNLogEntry logEntry) throws SVNException {
      this.logEntry = logEntry;
    }
    /**
    * @return Returns the author.
    */
    public String getAuthor() {
      if (this.logEntry==null) {
        return null;
      }
      return this.logEntry.getAuthor();
    }
    /**
    * @return Returns the changedPaths.
    */
    public Map getChangedPaths() {
      if (this.logEntry==null) {
        return null;
      }
      return this.logEntry.getChangedPaths();
    }
    /**
    * @return Returns the date.
    */
    public Date getDate() {
      if (this.logEntry==null) {
        return null;
      }
      return this.logEntry.getDate();
    }
    /**
    * @return Returns the message.
    */
    public String getMessage() {
      if (this.logEntry==null) {
        return null;
      }
      return this.logEntry.getMessage();
    }
    /**
    * @return Returns the revision.
    */
    public long getRevision() {
      if (this.logEntry==null) {
        return 0;
      }
      return this.logEntry.getRevision();
    }
  }

  String convertFileSize(long filesize) {
    String strUnit="B";
    String strAfterComma="";
    int intDivisor=1;
    if(filesize>=1024*1024) {
      strUnit = "MB";
      intDivisor=1024*1024;
    } else if(filesize>=1024) {
      strUnit = "KB";
      intDivisor=1024;
    }
    if(intDivisor==1) return filesize + " " + strUnit;
    strAfterComma = "" + 100 * (filesize % intDivisor) / intDivisor ;
    if(strAfterComma=="") strAfterComma=".0";
    return filesize / intDivisor + "." + strAfterComma + " " + strUnit;
  }
  
  String paddedString(String str, int size) {
    if (str==null) {
      return "";
    }
    if (str.length()>size) {
      return str.substring(0, size);
    } else {
      return str;
    }
  }
  
%>
